package com.skivingcloud.report.datas.service.impl;

import com.skivingcloud.common.exception.BusinessException;
import com.skivingcloud.report.datas.entity.ReportDatasetParam;
import com.skivingcloud.report.datas.mapper.ReportDatasetParamMapper;
import com.skivingcloud.report.datas.service.IReportDatasetParamService;
import com.skivingcloud.report.utils.ParamsResolverHelper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-07-20
 */
@Service
public class ReportDatasetParamServiceImpl extends ServiceImpl<ReportDatasetParamMapper, ReportDatasetParam> implements IReportDatasetParamService {
	
    private ScriptEngine engine;
    {
        ScriptEngineManager manager = new ScriptEngineManager();
        engine = manager.getEngineByName("JavaScript");
    }
    
    @Override
	public String transform(Map<String, Object> contextData, String dynSentence) {
		if (StringUtils.isBlank(dynSentence)) {
            return dynSentence;
        }
        if (dynSentence.contains("${")) {
            dynSentence = ParamsResolverHelper.resolveParams(contextData, dynSentence);
        }
        if (dynSentence.contains("${")) {
            throw new BusinessException("参数替换失败：" + dynSentence);
        }
        return dynSentence;
	}

    @Override
    public String transform(List<ReportDatasetParam> dataSetParamList, String dynSentence) {
        Map<String, Object> contextData = new HashMap<>();
        if (null == dataSetParamList || dataSetParamList.isEmpty()) {
            return dynSentence;
        }
        dataSetParamList.forEach(dataSetParamDto -> {
            contextData.put(dataSetParamDto.getParamName(), dataSetParamDto.getSampleItem());
        });
        return transform(contextData, dynSentence);
    }

    @Override
    public Object verification(ReportDatasetParam param) throws Exception {
        String validationRules = param.getValidationRules();
        if (StringUtils.isNotBlank(validationRules)) {
            try {
                engine.eval(validationRules);
                if(engine instanceof Invocable){
                    Invocable invocable = (Invocable) engine;
                    Object exec = invocable.invokeFunction("verification", param);
                    ObjectMapper objectMapper = new ObjectMapper();
                    if (exec instanceof Boolean) {
                        return objectMapper.convertValue(exec, Boolean.class);
                    }else {
                        return objectMapper.convertValue(exec, String.class);
                    }

                }

            } catch (Exception ex) {
                throw ex;
            }

        }
        return true;
    }
}
